<template>
  <div>
    <el-button class="abtn" type="primary" @click="dialogFormVisible=true;formData={}">添加</el-button>
    <el-input v-model="formData.username" placeholder="用户名" class="handle-input mr10"></el-input>
    <el-button type="primary" icon="el-icon-search" @click="handleSearch(formData.username)">搜索</el-button>
    <el-table :data="list" border style="width: 100%">
      <el-table-column fixed prop="uid" label="编号" />
      <el-table-column fixed prop="uname" label="姓名" />
      <el-table-column fixed prop="username" label="用户名" />
      <el-table-column fixed label="性别" >
        <template slot-scope="scope">
          {{ scope.row.sex == 1 ? "男" : "女" }}
        </template>
      </el-table-column>
      <el-table-column fixed prop="address" label="地址" />
      <el-table-column fixed prop="createtime" label="创建时间" />
      <el-table-column fixed prop="phone" label="联系方式" width="108"/>
      <el-table-column fixed prop="email" label="邮箱"/>
      <el-table-column fixed prop="rid" label="角色编号"/>
      <el-table-column fixed prop="picture" label="头像"/>
      <el-table-column label="状态" align="center" width="101">
        <template #default="scope">
          <el-tag>{{ scope.row.state == 1 ? '启用':'禁用' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" size="small" icon="el-icon-edit" @click="editInfo(scope.row)">编辑</el-button>
          <el-button type="text" size="small" icon="el-icon-delete" class="red" @click="remove(scope.row.uid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="formData">
        <el-form-item label="编号" :label-width="formLabelWidth" >
          <el-input v-model="formData.uid" :disabled="true"/>
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="formData.uname"/>
        </el-form-item>
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="formData.username"/>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="formData.password" show-password/>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-radio-group v-model="formData.sex">
            <el-radio label="男" value="1"/>
            <el-radio label="女" value="0"/>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="地址" :label-width="formLabelWidth">
          <el-input v-model="formData.address"/>
        </el-form-item>
        <el-form-item label="创建时间" :label-width="formLabelWidth">
          <div class="block">
            <el-date-picker v-model="formData.createdate" type="date" placeholder="选择入职日期"/>
          </div>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth">
          <el-input v-model="formData.phone"/>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="formData.email"/>
        </el-form-item>
        <el-form-item label="角色编号" :label-width="formLabelWidth">
          <el-input v-model="formData.rid"/>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-input v-model="formData.state"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submits()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Emp',
  data () {
    return {
      list: [],
      title: '添加用户',
      dialogFormVisible: false,
      formLabelWidth: '120px',
      formData: {}
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.$http.get('http://localhost:8081/user/list').then(data => {
        this.list = data
      })
    },
    handleSearch (username) {
      console.log(username)
      if (username === undefined || username === '') {
        this.$http.get('http://localhost:8081/user/list').then(data => {
          this.list = data
        })
      } else {
        this.$http.get(`http://localhost:8081/user/listWrapper?username=${username}`).then(data => {
          this.list = data
        })
      }
    },
    remove (uid) {
      alert(uid)
      this.$http.get(`http://localhost:8081/user/remove?uid=${uid}`).then(data => {
        console.log(data)
        if (data === true) {
          this.$message.success('删除成功')
          this.getList()
        } else {
          this.$message.error('删除失败')
        }
      })
    },
    editInfo (rowInfo) {
      this.dialogFormVisible = true
      this.formData = rowInfo
      this.title = '修改信息'
    },
    submits () {
      if (this.formData.uid === undefined) {
        this.$http.get('http://localhost:8081/user/save', {params: this.formData}).then(data => {
          console.log(data)
          if (data === true) {
            this.$message.success('添加成功')
            this.getList()
            this.dialogFormVisible = false
          } else {
            this.$message.error('添加失败')
            this.dialogFormVisible = false
          }
        })
      } else {
        this.$http.get('http://localhost:8081/user/update', {params: this.formData}).then(data => {
          console.log(data)
          if (data === true) {
            this.$message.success('修改成功')
            this.getList()
            this.dialogFormVisible = false
          } else {
            this.$message.error('修改失败')
            this.dialogFormVisible = false
          }
        })
      }
    }
  }
}
</script>

<style scoped>
.abtn{
  float: left;
}
.red {
  color: #ff0000;
}
.handle-input {
  width: 300px;
  display: inline-block;
}
.mr10 {
  margin-right: 10px;
}
</style>
